package until;

import config.commonconfig;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.testng.annotations.DataProvider;

import java.util.*;

import static config.commonconfig.*;
import static io.restassured.RestAssured.given;

/**
 * @Description
 * @auther lileilei
 * @create 2020-01-04 14:11
 */
public class CasesDataProvider {
    @DataProvider(name = "casesProvider")
    public static Object[][] caseProvider() {
        RestAssured.baseURI = commonconfig.url;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("project", projectname);
        map.put("test", projecttest);
        Response response = given().params(map)
                .contentType("application/json;charset=UTF-8")
                .request()
                .get(appCasepath);
        StringJsonParson stringJsonParson = new StringJsonParson();
        List<Map<String, Object>> cases = (List<Map<String, Object>>) stringJsonParson.json2map(response.asString()).get("data");
        return listtome(cases);
    }

    private static Object[][] listtome(List<Map<String, Object>> cases) {
        String[][] z = new String[cases.size()][];

        for (int i = 0; i < z.length; i++) {

            Map m = (Map) cases.get(i);
            Set set = m.keySet();
            z[i] = new String[m.size()];
            Iterator it = set.iterator();

            for (int j = 0; it.hasNext(); j++) {
                String s = (String) it.next();
                if (m.get(s) != null) {
                    z[i][j] = m.get(s).toString();
                }
            }
        }
        return z;
    }

    public static void main(String[] args) {
        CasesDataProvider casesDataProvider = new CasesDataProvider();
        System.out.println(casesDataProvider.caseProvider());
    }
}
